package procsim;

import java.awt.*;
import java.awt.image.*;
import java.util.*;

public class GBus extends Bus implements GraphicalLine {

    private LinkedHashMap<Coords, ImageObserver> coords;
    private Color color;
    private String name;
    private boolean complement = false;

    public GBus(String name) {
        super();
        color = Color.GRAY;
        this.name = name;
        coords = new LinkedHashMap<Coords, ImageObserver>();
    }

    public GBus(String name, Color color) {
        super();
        this.name = name;
        this.color = color;
        coords = new LinkedHashMap<Coords, ImageObserver>();
    }

    public GBus(int num, String name) {
        super(num);
        if(num>1) color = Color.GRAY;
        else color = Color.BLUE;
        this.name = name;
        coords = new LinkedHashMap<Coords, ImageObserver>();
    }

    public GBus(int num, String name, Color color) {
        super(num);
        this.name = name;
        this.color = color;
        coords = new LinkedHashMap<Coords, ImageObserver>();
    }

    private Color getColor(){
        if(result().getNumOfBits()==1){
            if(result().isNull())
                return Color.BLUE;
            else
                return Color.RED;
        }
        else return color;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public GBus addCoords(ImageObserver observer, Coords coor) {
        coords.put(coor, observer);
        return this;
    }

    @Override
    public GBus addCoords(ImageObserver observer, int x1, int y1, int x2, int y2) {
        coords.put(new Coords(x1, y1, x2, y2), observer);
        return this;
    }

    @Override
    public void drawIt(Graphics2D g, ImageObserver observer) {
        Util.saveGraphics(g);
        boolean firstpass = true;
        Iterator iter = coords.keySet().iterator();
        while(iter.hasNext()) {
            Coords drawing = (Coords)iter.next();
            ImageObserver tmp = coords.get(drawing);
            if(tmp!=observer) continue;
            Color tmpcolor = g.getColor();
            g.setColor(getColor());
            g.setStroke(getStroke());
            g.drawLine(drawing.getX1(), drawing.getY1(), drawing.getX2(), drawing.getY2());
            g.setColor(Color.GRAY);
            if(firstpass){
                g.drawString(name, drawing.getX1()+2, drawing.getY1()-2);
                if(result().getNumOfBits()>1)
                    g.drawString(Util.int2Hex(result().get(), getNumOfBits()), drawing.getX1()+2, drawing.getY1()+12);
                g.setStroke(new BasicStroke(1));
                if(complement) {
                    int len = name.length();
                    g.drawLine(drawing.getX1()+1, drawing.getY1()-13, drawing.getX1()+1+len*8, drawing.getY1()-13);
                }
                firstpass=false;
            }
            g.setColor(tmpcolor);
        }
        Util.restoreGraphics(g);
    }

    private BasicStroke getStroke(){
        int width;
        if(getNumOfBits()==1) width = 1;
        else width = 2;
        return new BasicStroke(width);
    }

    public void setComplement(){
        complement = true;
    }
}
